TOOL.Category		= "Constraints"
TOOL.Name			= "Easy Alignment"
TOOL.Command		= nil
TOOL.ConfigName		= nil

TOOL.ClientConVar[ "align_x" ] = 1
TOOL.ClientConVar[ "align_y" ] = 0
TOOL.ClientConVar[ "align_z" ] = 1
TOOL.ClientConVar[ "freeze" ] = 1
TOOL.ClientConVar[ "nocollide" ] = 0
TOOL.ClientConVar[ "rotation" ] = 1

if ( CLIENT ) then
	language.Add( "Tool_easyalignment_name", "Easy Alignment" )
	language.Add( "Tool_easyalignment_desc", "Aligns one prop with another." )
	language.Add( "Tool_easyalignment_0", "Primary: Align with Selected Prop, Secondary: Select Prop, Reload: Deselect Prop" )

	language.Add( "easyalignment_axes_label", "\nAxes to Align:" )
	language.Add( "easyalignment_x_label", "X" )
	language.Add( "easyalignment_y_label", "Y" )
	language.Add( "easyalignment_z_label", "Z" )

	language.Add( "easyalignment_other_label", "\nOther Options:" )
	language.Add( "easyalignment_freeze_label", "Freeze Props" )
	language.Add( "easyalignment_nocollide_label", "NoCollide Props" )
	language.Add( "easyalignment_rotation_label", "Match Rotation" )

	language.Add("easyalignment_noselect", "No prop selected!" )
	language.Add("easyalignment_player", "That's another player!" )
	language.Add("easyalignment_worldspawn", "That's part of the level!" )
end

local Prop1 = { }
Prop1.ent = nil
Prop1.r, Prop1.g, Prop1.b, Prop1.a = 255, 255, 255, 255
Prop1.pos = Vector( 0, 0, 0 )

local Prop2 = { }
Prop2.ent = nil
Prop2.pos = Vector( 0, 0, 0 )
local propSelected = false

function TOOL:LeftClick( trace )
	if CLIENT then return true end
	--if not Prop1.ent:IsValid() then UnSelect() end
	Prop2.ent = trace.Entity

	local class = Prop2.ent:GetClass()
	if class == "worldspawn" then self:GetOwner():PrintMessage( HUD_PRINTTALK, "#easyalignment_worldspawn" ) return true end
	if class == "player" then self:GetOwner():PrintMessage( HUD_PRINTTALK, "#easyalignment_player" ) return true end

	if not propSelected then self:GetOwner():PrintMessage( HUD_PRINTTALK, "#easyalignment_noselect" ) return true end

	if ( self:GetClientNumber( "freeze" ) == 1 ) then Prop2.ent:GetPhysicsObject():EnableMotion( false ) end
	if ( self:GetClientNumber( "nocollide" ) == 1 ) then constraint.NoCollide( Prop1.ent, Prop2.ent, 0, 0 ) end
	if ( self:GetClientNumber( "rotation" ) == 1 ) then Prop2.ent:SetAngles( Prop1.ent:GetAngles() ) end

	Prop1.pos = Prop1.ent:GetPos()
	Prop2.pos = Prop2.ent:GetPos()
	if ( self:GetClientNumber( "align_x" ) == 1 ) then Prop2.pos.x = Prop1.pos.x end
	if ( self:GetClientNumber( "align_y" ) == 1 ) then Prop2.pos.y = Prop1.pos.y end
	if ( self:GetClientNumber( "align_z" ) == 1 ) then Prop2.pos.z = Prop1.pos.z end
	Prop2.ent:SetPos( Prop2.pos )
	return true
end

function TOOL:RightClick( trace )
	if CLIENT then return true end

	if propSelected then UnSelect() end
	Prop1.ent = trace.Entity

	local class = Prop1.ent:GetClass()
	if class == "worldspawn" then self:GetOwner():PrintMessage( HUD_PRINTTALK, "#easyalignment_worldspawn" ) return true end
	if class == "player" then self:GetOwner():PrintMessage( HUD_PRINTTALK, "#easyalignment_player" ) return true end

	if ( self:GetClientNumber( "freeze" ) == 1 ) then Prop1.ent:GetPhysicsObject():EnableMotion( false ) end

	Prop1.r, Prop1.g, Prop1.b, Prop1.a = Prop1.ent:GetColor()
	Prop1.ent:SetColor( 0, 255, 0, 255)
	propSelected = true
	return true
end

function TOOL:Holster()
	if propSelected then UnSelect() end
end

function TOOL:Reload()
	if propSelected then UnSelect() end
end

function UnSelect()
	if Prop1.ent:IsValid() then Prop1.ent:SetColor( Prop1.r, Prop1.g, Prop1.b, Prop1.a ) end
	Prop1.ent = nil
	Prop1.r, Prop1.g, Prop1.b, Prop1.a = 255, 255, 255, 255
	Prop1.pos = Vector( 0, 0, 0 )
	propSelected = false
end

function TOOL.BuildCPanel(CPanel)
	CPanel:AddControl("Header", {
		Text = "#Tool_easyalignment_name",
		Description = "#Tool_easyalignment_desc"
	})

	CPanel:AddControl("Label", {
		Text = "#easyalignment_axes_label"
	})

	CPanel:AddControl("CheckBox", {
		Label = "#easyalignment_x_label",
		Command = "easyalignment_align_x"
	})

	CPanel:AddControl("CheckBox", {
		Label = "#easyalignment_y_label",
		Command = "easyalignment_align_y"
	})

	CPanel:AddControl("CheckBox", {
		Label = "#easyalignment_z_label",
		Command = "easyalignment_align_z"
	})

	CPanel:AddControl("Label", {
		Text = "#easyalignment_other_label"
	})

	CPanel:AddControl("CheckBox", {
		Label = "#easyalignment_freeze_label",
		Command = "easyalignment_freeze"
	})

	CPanel:AddControl("CheckBox", {
		Label = "#easyalignment_nocollide_label",
		Command = "easyalignment_nocollide"
	})

	CPanel:AddControl("CheckBox", {
		Label = "#easyalignment_rotation_label",
		Command = "easyalignment_rotation"
	})
end
